
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>15.3d图形</title>
	<style>
		canvas {
            background-color: rebeccapurple;
		}
	</style>
</head>
<body>
	<canvas id="gl" style="background-color: rebeccapurple;"></canvas>
	<script type="x-shader/x-vertex" id="vertex">
		attribute vec4 a_position;
		attribute vec4 a_color;
		varying vec4 aa;
		void main() {
			vec3 r=a_position.xyz/1000.0*2.0-1.0;
			aa=a_color;
			gl_Position = vec4(r.x+1.1,r.y+1.1,r.z,1);
		}
	</script>
	
	<script type="x-shader/x-fragment" id="fragment">
		precision mediump float;
		varying vec4 aa;
		void main(){
			gl_FragColor =aa;
		}
	</script>
	<script>
		 /** @type {HTMLCanvasElement} */   
		const canvas=document.getElementById("gl")
		const gl=canvas.getContext("webgl")
		canvas.width=1000
		canvas.height=1000
		//流程 1.创建着色器 编译 链接
		const vertex=document.getElementById("vertex").textContent
		const fragment=document.getElementById("fragment").textContent

		const vertexShader=gl.createShader(gl.VERTEX_SHADER)
		const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)
		gl.shaderSource(vertexShader,vertex)
		gl.shaderSource(fragmentShader,fragment)
		gl.compileShader(vertexShader)
		gl.compileShader(fragmentShader)
		const program=gl.createProgram()
		gl.attachShader(program,vertexShader)
		gl.attachShader(program,fragmentShader)
		gl.linkProgram(program)
		gl.useProgram(program)


		const a_position=gl.getAttribLocation(program,"a_position")
		const bufferData=gl.createBuffer()
		gl.bindBuffer(gl.ARRAY_BUFFER,bufferData)
		const p=[
          // left column front
            0,   0,  0,
           30,   0,  0,
            0, 150,  0,
            0, 150,  0,
           30,   0,  0,
           30, 150,  0,

          // top rung front
           30,   0,  0,
          100,   0,  0,
           30,  30,  0,
           30,  30,  0,
          100,   0,  0,
          100,  30,  0,

          // middle rung front
           30,  60,  0,
           67,  60,  0,
           30,  90,  0,
           30,  90,  0,
           67,  60,  0,
           67,  90,  0,
]
		gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(p),gl.STATIC_DRAW)
		gl.enableVertexAttribArray(a_position)
		
		gl.vertexAttribPointer(a_position,3,gl.FLOAT,false,0,0)

		gl.viewport(0,0,1000,1000)


		var colorLocation = gl.getAttribLocation(program, "a_color");

    // Turn on the color attribute
    gl.enableVertexAttribArray(colorLocation);

	var colorBuffer = gl.createBuffer();
  // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = colorBuffer)
  gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
  gl.bufferData(
      gl.ARRAY_BUFFER,
      new Uint8Array([
          // left column front
        200,  70, 120,
        200,  70, 120,
        200,  70, 3,
        44,  70, 2,
        200,  70, 120,
        200,  70, 120,

          // top rung front
        223,  70, 120,
        200,  70, 1,
        200,  70, 3,
        111,  70, 120,
        200,  70, 2,
        200,  70, 120,

          // middle rung front
        1,  70, 120,
        200,  70, 120,
        123,  70, 120,
        200,  70, 120,
        33,  70, 120,
        3,  70, 120
	]),
      gl.STATIC_DRAW);
    gl.vertexAttribPointer(colorLocation, 3, gl.UNSIGNED_BYTE, true, 0, 0);

	gl.drawArrays(gl.TRIANGLE_STRIP, 0, p.length/3);
	//画线
	</script>
</body>
</html>